home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #5 / Amiga Plus CD - 2000 - No. 5.iso / Tools / Grafik / Misc / netpbm / bin / anytopnm next >
Encoding:
Text File  |  2000-01-01  |  5.6 KB  |  266 lines

  1. #!/bin/sh
  2. #
  3. # anytopnm - attempt to convert an unknown type of image file to a P?M file.
  4. #
  5. # Copyright (C) 1991 by Jef Poskanzer.
  6. #
  7. # Permission to use, copy, modify, and distribute this software and its
  8. # documentation for any purpose and without fee is hereby granted, provided
  9. # that the above copyright notice appear in all copies and that both that
  10. # copyright notice and this permission notice appear in supporting
  11. # documentation.  This software is provided "as is" without express or
  12. # implied warranty.
  13.  
  14. if [ $# -gt 1 ] ; then
  15.     echo "Usage: $0 infilename > outfilename.pnm" 1>&2
  16.     echo "       $0 - < infilename > outfilename.pnm" 1>&2
  17.     echo "  This will convert the input file into a PNM file" 1>&2
  18.     echo "  It automatically determines the format of the input file" 1>&2
  19.     echo "    with the 'file' command, and acts accordingly" 1>&2
  20.     echo "  If the infilename is a '-', uses standard input" 1>&2
  21.     exit 1
  22. fi
  23.  
  24. tmpfiles=""
  25.  
  26. # Take out all spaces
  27. fileextension="${1// /}"
  28. # Find the filename extension for last-ditch efforts later
  29. fileextension="${fileextension/#*./.}"
  30.  
  31. # Sanitize the filename by making our own temporary files as safely as
  32. # possible.
  33. file="/tmp/atn.stdin.$$"
  34. rm -f "$file"
  35. if [ $# -eq 0 -o "$1" = "-" ] ; then
  36.     cat > "$file"
  37. else
  38.     if [ ! -e "$1" ] ; then
  39.         echo "$0: $1: No such file" 1>&2
  40.         exit 1
  41.     fi
  42.     
  43.     if [ ! -f "$1" ] ; then
  44.         echo "$0: $1: Not a file" 1>&2
  45.         exit 1
  46.     fi
  47.     
  48.     if [ ! -r "$1" ] ; then
  49.         echo "$0: $1: Not a readable file" 1>&2
  50.         exit 1
  51.     fi
  52.     
  53.     if [ -z "$1" ] ; then
  54.         echo "$0: $1: Empty file" 1>&2
  55.         exit 1
  56.     fi
  57.     
  58.     cat < "$1" > "$file"
  59. fi
  60.  
  61. tmpfiles="$tmpfiles $file"
  62.  
  63.  
  64.  
  65. filetype=`file "$file" | cut -d: -f2-`
  66.  
  67. case "$filetype" in
  68.  
  69.     *PBM* | *PGM* | *PPM* )
  70.     cat "$file"
  71.     ;;
  72.  
  73.     *uuencoded* )
  74.     newfile="/tmp/atn.decode.$$"
  75.     rm -f "$newfile"
  76.     (echo begin 600 $newfile; tail +2 < "$file") | uudecode
  77.     tmpfiles="$tmpfiles $newfile"
  78.     anytopnm "$newfile"
  79.     ;;
  80.  
  81.     *bzip2*compressed*data* )
  82.     bzip2 -dk < "$file" | anytopnm -
  83.     ;;
  84.  
  85.     *bzip*compressed*data* )
  86.     bzip -dk < "$file" | anytopnm -
  87.     ;;
  88.  
  89.     *gzip*compressed*data* )
  90.     gzip --decompress --to-stdout < "$file" | anytopnm -
  91.     ;;
  92.  
  93.     *compress* )
  94.     uncompress -c < "$file" | anytopnm -
  95.     ;;
  96.  
  97.     *btoa* )
  98.     atob < "$file" | anytopnm -
  99.     ;;
  100.  
  101.     *Sun* | *rasterfile* )
  102.     rasttopnm "$file"
  103.     ;;
  104.  
  105.     *GIF* )
  106.     giftopnm "$file"
  107.     ;;
  108.  
  109.     *TIFF* )
  110.     tifftopnm "$file"
  111.     ;;
  112.  
  113.     *IFF*ILBM* )
  114.     ilbmtoppm "$file"
  115.     ;;
  116.  
  117.     *Lisp* )
  118.     lispmtopgm "$file"
  119.     ;;
  120.  
  121.     *PC*Paintbrush* )
  122.     pcxtoppm "$file"
  123.     ;;
  124.  
  125.     *Bennet* )
  126.     ybmtopbm "$file"
  127.     ;;
  128.  
  129.     *pixmap*image*text* )
  130.     xpmtoppm < "$file"
  131.     ;;
  132.  
  133.     # This has to come after all other 'text' files, or you may be
  134.     # disappointed.
  135.     *text* )
  136.     pbmtext -builtin fixed < "$file"
  137.     ;;
  138.  
  139.     *JPEG* | *JFIF* )
  140.     jpegtopnm "$file"
  141.     ;;
  142.  
  143.     *PNG* )
  144.     pngtopnm "$file"
  145.     ;;
  146.  
  147.     *MicroDesign* )
  148.     mdatopbm -d -- "$file"
  149.     ;;
  150.  
  151.     *PC*bitmap*data* )
  152.     bmptoppm "$file"
  153.     ;;
  154.     
  155.     * )
  156.     # Can't figure out the file type from the magic number,
  157.     # try the extension.
  158.     case "$fileextension" in
  159.  
  160.         *.pbm | *.pbm.* | *.pgm | *.pgm.* | *.ppm | *.ppm.* )
  161.         cat "$file"
  162.         ;;
  163.         *.x | *.x.* | *.xbm | *.xbm.* | *.x10bm | *.x10bm.* | \
  164.             *.x11bm | *.x11bm.* | *.bitmap | *.bitmap.* )
  165.         xbmtopbm "$file"
  166.         ;;
  167.         *.r | *.r.* | *.rast | *.rast.* )
  168.         rasttopnm "$file"
  169.         ;;
  170.         *.mac | *.mac.* | *.macp | *.macp.* )
  171.         macptopbm "$file"
  172.         ;;
  173.         *.g3 | *.g3.* | *.fax | *.fax.* )
  174.         g3topbm "$file"
  175.         ;;
  176.         *.xwd | *.xwd.* | *.x10wd | *.x10wd.* | *.x11wd | *.x11wd.* )
  177.         xwdtopnm "$file"
  178.         ;;
  179.         *.brush | *.brush.* )
  180.         brushtopbm "$file"
  181.         ;;
  182.         *.img | *.img.* )
  183.         gemtopbm "$file"
  184.         ;;
  185.         *.pcx | *.pcx.* )
  186.         pcxtoppm "$file"
  187.         ;;
  188.         *.pic | *.pic.* | *.pict | *.pict.* | *.pict2 | *.pict2.* )
  189.         picttoppm "$file"
  190.         ;;
  191.         *.tif | *.tif.* | *.tiff | *.tiff.* )
  192.         tifftopnm "$file"
  193.         ;;
  194.         *.fs | *.fs.* | *.face | *.face.* )
  195.         fstopgm "$file"
  196.         ;;
  197.         *.hips | *.hips.* )
  198.         hipstopgm "$file"
  199.         ;;
  200.         *.fits | *.fits.* )
  201.         fitstopnm "$file"
  202.         ;;
  203.         *.gif | *.gif.* )
  204.         giftopnm "$file"
  205.         ;;
  206.         *.iff | *.iff.* | *.ilbm | *.ilbm.* )
  207.         ilbmtoppm "$file"
  208.         ;;
  209.         *.lispm | *.lispm.* )
  210.         lispmtopgm "$file"
  211.         ;;
  212.         *.mtv | *.mtv.* )
  213.         mtvtoppm "$file"
  214.         ;;
  215.         *.qrt | *.qrt.* )
  216.         qrttoppm "$file"
  217.         ;;
  218.         *.tga | *.tga.* | *.targa | *.targa.* )
  219.         tgatoppm "$file"
  220.         ;;
  221.         *.xim | *.xim.* )
  222.         ximtoppm "$file"
  223.         ;;
  224.         *.xpm | *.xpm.* | *.xpm2 | *.xpm2.* )
  225.         xpmtoppm "$file"
  226.         ;;
  227.         *.pi1 | *.pi1.* )
  228.         pi1toppm "$file"
  229.         ;;
  230.         *.pi3 | *.pi3.* )
  231.         pi3topbm "$file"
  232.         ;;
  233.         *.spu | *.spu.* )
  234.         sputoppm "$file"
  235.         ;;
  236.         *.spc | *.spc.* )
  237.         spctoppm "$file"
  238.         ;;
  239.         *.ybm | *.ybm.* | *.face | *.face.* )
  240.         ybmtopbm "$file"
  241.         ;;
  242.         *.JPEG | *.jpeg | *.jpg | *.JPG )
  243.         jpegtopnm "$file"
  244.         ;;
  245.         *.png | *.PNG )
  246.         pngtopnm "$file"
  247.         ;;
  248.         *.mda | *.mdp )
  249.         mdatopbm -d -- "$file"
  250.         ;;
  251.         * )
  252.         echo "$0: unknown file type: $filetype" 1>&2
  253.         exit 1
  254.         ;;
  255.  
  256.     esac
  257.     ;;
  258.  
  259. esac
  260.  
  261.  
  262. if [ "$tmpfiles" ] ; then
  263.     rm -f $tmpfiles
  264. fi
  265. exit 0
  266.